package com.example.hquandroidwork7;

import android.content.Context;
import android.graphics.Color;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.github.mikephil.charting.charts.LineChart;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.data.LineData;
import com.github.mikephil.charting.data.LineDataSet;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

@Route(path="/app/main/MainActivity")
public class MainActivity extends AppCompatActivity implements SensorEventListener {

    @BindView(R.id.chart)
    LineChart orientation_chart;
    @BindView(R.id.x_zhou)   //仰附角
    TextView x_axis;
    @BindView(R.id.y_zhou)    //翻转角
    TextView y_axis;
    @BindView(R.id.z_zhou)  //指向地心的方位角
    TextView z_axis;
    private SensorManager sensorManager=null;
    private int timeIndex=0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ARouter.openLog();
        ARouter.openDebug();
        ARouter.init(getApplication());
        ButterKnife.bind(this);

        sensorManager=(SensorManager) getSystemService(Context.SENSOR_SERVICE);//获取传感器管理服务


        orientation_chart.setTouchEnabled(false);
        orientation_chart.getXAxis().setEnabled(true);
        orientation_chart.getXAxis().setPosition(XAxis.XAxisPosition.BOTTOM);
        orientation_chart.getAxisLeft().setEnabled(true);
        orientation_chart.getAxisRight().setEnabled(false);
        List<Entry> XEntries=new ArrayList<>();
        List<Entry> YEntries=new ArrayList<>();
        List<Entry> ZEntries=new ArrayList<>();
        LineDataSet XDataset=new LineDataSet(XEntries,"X");
        LineDataSet YDataset=new LineDataSet(YEntries,"Y");
        LineDataSet ZDataset=new LineDataSet(ZEntries,"Z");
        XDataset.setDrawCircles(false);
        YDataset.setDrawCircles(false);
        ZDataset.setDrawCircles(false);
        XDataset.setColor(Color.rgb(255,0,0));
        YDataset.setColor(Color.rgb(0,255,0));
        ZDataset.setColor(Color.rgb(0,0,255));
        LineData lineData=new LineData();
        lineData.addDataSet(XDataset);
        lineData.addDataSet(YDataset);
        lineData.addDataSet(ZDataset);
        orientation_chart.setData(lineData);
        Log.d("初始化","完成");


        //获取本机所有可使用的传感器服务
        List<Sensor> sensor_list;
        sensor_list=sensorManager.getSensorList(Sensor.TYPE_ALL);
        for(Sensor sensor: sensor_list){
            Log.d("sensor",sensor.getName());
        }

    }


    @Override
    protected void onPostResume(){
        super.onPostResume();
        sensorManager.registerListener(this,sensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION),SensorManager.SENSOR_DELAY_FASTEST);

    }

    @Override
    protected void onPause(){
        super.onPause();
        sensorManager.unregisterListener(this);

    }

    @Override
    protected void onStop(){
        super.onStop();
        sensorManager.unregisterListener(this);

    }

    @Override
    public void onSensorChanged(SensorEvent sensorEvent) {
        float[] values = sensorEvent.values;
        z_axis.setText("方位角----Z轴：    " + values[0]);
        x_axis.setText("倾斜角----X轴：    " + values[1]);
        y_axis.setText("滚动角----Y轴：    " + values[2]);

        Log.d("values数组",values[0]+" "+values[1]+" "+values[2]);
        LineData lineData = orientation_chart.getLineData();
        if (lineData != null) {
            LineDataSet XLineDataSet = (LineDataSet) lineData.getDataSetByLabel("X", true);
            LineDataSet YLineDataSet = (LineDataSet) lineData.getDataSetByLabel("Y", true);
            LineDataSet ZLineDataSet = (LineDataSet) lineData.getDataSetByLabel("Z", true);
            XLineDataSet.addEntry(new Entry(timeIndex++, values[1]));
            YLineDataSet.addEntry(new Entry(timeIndex++, values[2]));
            ZLineDataSet.addEntry(new Entry(timeIndex++, values[0]));
            if (XLineDataSet.getEntryCount() < 200) {
                orientation_chart.getXAxis().setAxisMaximum(200);
            } else {
                XLineDataSet.removeFirst();
                YLineDataSet.removeFirst();
                ZLineDataSet.removeFirst();
                orientation_chart.getXAxis().setAxisMaximum(XLineDataSet.getXMax());
            }
            orientation_chart.getAxisLeft().setAxisMaximum(Math.max(ZLineDataSet.getYMax(),XLineDataSet.getYMax()));
            orientation_chart.getAxisLeft().setAxisMinimum(Math.min(XLineDataSet.getYMin(),ZLineDataSet.getYMin()));
            orientation_chart.getXAxis().setAxisMinimum(XLineDataSet.getXMin());
            orientation_chart.notifyDataSetChanged();
            orientation_chart.invalidate();

        }

    }

    @Override
    public void onAccuracyChanged(Sensor sensor, int accuracy) {

    }


}
